package com.ruitu.health.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.health.R;
import com.ruitu.health_1.ui.ArticleDetailsActivity;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.home.HomeData;
import com.ruitu.router_module.support.view.FooterHolder;
import com.ruitu.router_module.util.GlideUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class HomePro2Fragment extends BaseFragment {
    RecyclerView rcv_list;

    HomeProListAdapter adapter;
    private List<Article> articleList = new ArrayList<>();

    public static HomePro2Fragment newInstance() {
        Bundle args = new Bundle();
        HomePro2Fragment fragment = new HomePro2Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Arad.bus.register(this);
        adapter = new HomeProListAdapter(getActivity());

        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", articleList.get(position).getId());
                startActivity(ArticleDetailsActivity.class, bundle);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMsg(EventModel ev) {
        if (ev.getEventCode() == 19110752) {
            HomeData homeData = (HomeData) ev.getEventObj();
//            articleList = homeData.getArticleList();
            adapter.setDataList(articleList);
        }

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home_pro_2, container, false);

        rcv_list = v.findViewById(R.id.rcv_list);
        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

//        rcv_list.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
//
//            @Override
//            public void onLoadMore() {
//                // 加载更多
//                ToastUtils.showShortSafe("加载更多");
//            }
//        });

//        ((HomeFragment) getParentFragment()).setOnScrollListener(new HomeFragment.OnScrollListener() {
//            @Override
//            public void onScroll(ScrollInfo scrollInfo) {
//                if (scrollInfo.isScrollUp() && scrollInfo.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
//                }
//                if (scrollInfo.isScrollUp()) {
//                    int lastItemPosition = manager.findLastCompletelyVisibleItemPosition();
//                    int itemCount = manager.getItemCount();
//                    // 判断是否滑动到了最后一个item，并且是向上滑动
//                    if (lastItemPosition == (itemCount - 1)) {
//                        //加载更多
//                    }
//                    Logg.i("lastItemPosition = " + lastItemPosition + ",currPosition = " + scrollInfo.getCurrPosition() + ",dY = " + scrollInfo.getdY());
//                }
//            }
//        });

        return v;
    }

    private class HomeProListAdapter extends ListBaseAdapter<Article> {

        public HomeProListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_home_tab_pro_2;
        }

        @Override
        public int getItemCount() {
            return super.getItemCount();
        }

        @Override
        public int getItemViewType(int position) {
            if (position == mDataList.size()) {//最后一个 是底部item
                return 1;
            } else {
                return 0;
            }
        }

        @Override
        public SuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == 1) {
                View view = View.inflate(getActivity(), R.layout.footer_layout, null);
                FooterHolder footerHolder = new FooterHolder(view);
                return footerHolder;
            } else {
                return super.onCreateViewHolder(parent, viewType);
            }
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            if (holder instanceof FooterHolder) {
                TextView tv = holder.getView(R.id.tv_txt);
//                if () {
//                    tv.setText("加载完成");
//                } else {
//                    tv.setText("加载中...");
//                }
            } else {
                ImageView icon = holder.getView(R.id.icon);// 图片
                TextView tv_title = holder.getView(R.id.tv_title);// 标题
                TextView tv_name = holder.getView(R.id.tv_name);// 艺术家
                TextView tv_time = holder.getView(R.id.tv_time);// 时间
                TextView tv_subtitle = holder.getView(R.id.tv_subtitle);// 简介

                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
                params.width = params.height * 16 / 12;
                icon.setLayoutParams(params);

                Article article = mDataList.get(position);
                tv_title.setText(article.getTitle());
                tv_name.setText("" + article.getArtName());
                try {
//                    tv_time.setText(article.getUpdateTime());
                } catch (Exception e) {
                    tv_time.setText("");
                }
                tv_subtitle.setText(article.getReserveOne());

                GlideUtil.loadImg(article.getSmallImg(), icon);
            }
        }
    }
}
